package com.aj.service;

import com.aj.Interface.CheckService;
import com.aj.Interface.OrderSettingListService;
import com.aj.dao.MemberDao;
import com.aj.dao.OrderDao;
import com.aj.dao.OrderSettingListDao;
import com.aj.pojo.Member;
import com.aj.pojo.Order;
import com.aj.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    OrderSettingListDao orderSettingListDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,queryPageBean.getPageSize());

        Page<Map> page = orderSettingListDao.findpage(queryPageBean.getQueryString());
        System.out.println(page);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void add(Map map, Integer[] checkitemIds)  {
    String telephone=(String)map.get("telephone");
        Member memberByPhone=null;
    try {
        memberByPhone = findMemberByPhone(telephone);
    }catch (Exception e){
        //e.printStackTrace();
    }

        String name=(String)map.get("name");
        String orderDate=(String)map.get("orderDate");
        String orderType=(String)map.get("orderType");
        String orderStatus=(String)map.get("orderStatus");
        Date orderdate=null;
       try{orderdate= DateUtils.parseString2Date(orderDate);}catch (Exception e){
           e.printStackTrace();
           //注意日期格式
       }
        Date date=new Date();
        if(memberByPhone==null){
        memberByPhone=new Member();
       // member.setBirthday(date);
            memberByPhone.setName(name);
            memberByPhone.setRegTime(date);
            memberByPhone.setPhoneNumber(telephone);
            System.out.println(memberByPhone);
        createMember( memberByPhone);
        }
        for(Integer i:checkitemIds){
            Order order=new Order();
            order.setMemberId(memberByPhone.getId());
            order.setName(name);
            order.setOrderDate(orderdate);
            order.setTelephone(telephone);
            order.setSetmealId(i);
            if(orderStatus.equals("1")){
            order.setOrderStatus("未到诊");}else{
                order.setOrderStatus("已到诊");
            }
            if(orderType.equals("1")){
                order.setOrderType("微信预约");}else{
                order.setOrderType("电话预约");
            }
            orderDao.addorder(order);
        }

    }

    @Override
    public void delete(int id) {
    orderDao.delete(id);
    }

    @Override
    public List findSetmealId(int id) {
       return orderDao.findSetmealId(id);

    }

    @Override
    public void update(Map map, Integer[] checkitemIds) {
        orderDao.deleteByMemberIds((Integer)map.get("member_id"));
        for(Integer i:checkitemIds){
            Order order=new Order();
            order.setMemberId((Integer)map.get("member_id"));
            order.setName((String)map.get("name"));
          try {
              order.setOrderDate(DateUtils.parseString2Date((String)map.get("name")));
          }catch (Exception e){
              e.printStackTrace();
          }
            order.setTelephone((String)map.get("telephone"));
            order.setSetmealId(i);
            if(((String)map.get("orderStatus")).equals("1")){
                order.setOrderStatus("未到诊");}else{
                order.setOrderStatus("已到诊");
            }
            if(((String)map.get("orderType")).equals("1")){
                order.setOrderType("微信预约");}else{
                order.setOrderType("电话预约");
            }
            orderDao.addorder(order);
        }

    }

    public Member findMemberByPhone(String telephone){
        System.out.println(telephone);
        return memberDao.findMemberByTelephone(telephone);

    }
    public void createMember(Member member){

        //map.put("regTime",member.getRegTime());
        System.out.println(member);
        memberDao.addtoMember(member);

    }
    //public void addOrder(Order order){
//    orderDao.addorder(order);
//    }
}
